package com.example.questionnairespringboot.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnairespringboot.Req.PersonReq;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.questionnairespringboot.service.IConsumerService;
import com.example.questionnairespringboot.entity.Consumer;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-11-02
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Resource
    private IConsumerService consumerService;
    //  登录
    @PostMapping("/login")
    public boolean login(@RequestBody PersonReq personReq){
        if (StringUtils.isBlank(personReq.getAccount())||StringUtils.isBlank(personReq.getPassword())){
            return false;
        }
        return consumerService.login(personReq);
    }
    //注册
    @PostMapping("/register")
    public boolean register(@RequestBody Consumer c) {

        return consumerService.save(c);
    }
    // 新增和修改
    @PostMapping
    public Boolean save(@RequestBody Consumer consumer){

            // 新增或者更新
            return consumerService.saveOrUpdate(consumer);
    }

    // 删除
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id){
            return consumerService.removeById(id);
            }

    // 批删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids){
        return consumerService.removeByIds(ids);
        }

    // 查询所有数据
    @GetMapping("/")
    public List<Consumer> findAll(){
            return consumerService.list();
            }

    // 根据id查询
    @GetMapping("/{id}")
    public Consumer findOne(@PathVariable Integer id){
            return consumerService.getById(id);
            }

    // 分页查询
    @GetMapping("/page")
    public Page<Consumer> findPage(@RequestParam Integer pageNum,
    @RequestParam Integer pageSize){
            return consumerService.page(new Page<>(pageNum,pageSize));
            }

}

